package com.wehealth.three.lib_connect.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Environment;
import android.util.Log;


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;

@SuppressLint("SimpleDateFormat")
public class StringUtils {

    final static int LEADNUM = 12;

    public static SimpleDateFormat sdfm = new SimpleDateFormat("mm");
    public static SimpleDateFormat sdfH = new SimpleDateFormat("HH");
    public static SimpleDateFormat sdfD = new SimpleDateFormat("dd");
    public static SimpleDateFormat sdfYMDE = new SimpleDateFormat("yyyy-MM-dd EEE");// EEE代表星期几
    public static SimpleDateFormat sdfYMDHm = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    public static SimpleDateFormat sdfY = new SimpleDateFormat("yyyy");
    public static SimpleDateFormat sdfM = new SimpleDateFormat("MM");
    public static SimpleDateFormat sdfYM = new SimpleDateFormat("yyyy-MM");
    public static SimpleDateFormat sdfE = new SimpleDateFormat("EEE");// EEE代表星期几
    public static SimpleDateFormat sdfYMD = new SimpleDateFormat("yyyy-MM-dd");
    public static SimpleDateFormat sdfYMDHM = new SimpleDateFormat("yyyyMMddHHmm");
    public static SimpleDateFormat sdfYMDHMSS = new SimpleDateFormat("yyyyMMddHHmmss");
    private static String regEx = "[\\u4e00-\\u9fa5]";

    public static SimpleDateFormat sdf_yyyy_MM_dd_EEE = new SimpleDateFormat("yyyy-MM-dd EEE");// EEE代表星期几
    public static SimpleDateFormat sdf_m = new SimpleDateFormat("mm");
    public static SimpleDateFormat sdf_yyyy_MM_dd = new SimpleDateFormat("yyyy-MM-dd");
    public static SimpleDateFormat sdf_yyyyMMdd = new SimpleDateFormat("yyyyMMdd");
    public static SimpleDateFormat sdf_yyyy = new SimpleDateFormat("yyyy");
    public static SimpleDateFormat sdf_MM = new SimpleDateFormat("MM");
    public static SimpleDateFormat sdf_yyyyMMddHHmmss = new SimpleDateFormat("yyyyMMddHHmmss");
    public static SimpleDateFormat sdf_HHmmss = new SimpleDateFormat("HHmmss");
    public static SimpleDateFormat sdf_yyyy_MM_dd_HHmm = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    public static SimpleDateFormat sdf_yyyy_MM_dd_HHmm_EEE = new SimpleDateFormat("yyyy-MM-dd HH:mm EEE");
    public static SimpleDateFormat sdf_yyyy_MM_dd_HH_mm_ss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static SimpleDateFormat sdf_yyyy_MM_dd_HH_mm_ss_S = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    public static SimpleDateFormat sdf_s = new SimpleDateFormat("ss");
    public static SimpleDateFormat sdf_H = new SimpleDateFormat("HH");
    public static SimpleDateFormat sdf_D = new SimpleDateFormat("dd");
    public static SimpleDateFormat sdf_EEE = new SimpleDateFormat("EEE");// EEE代表星期几
    public static SimpleDateFormat sdf_HHmm = new SimpleDateFormat("HHmm");
    public static SimpleDateFormat sdf_HH_mm = new SimpleDateFormat("HH:mm");
    public static SimpleDateFormat sdf_MM_dd_HH_mm_ss = new SimpleDateFormat("MM-dd HH:mm:ss");
    public static final String ECG_PATIENT_INFO = "ECG_PATIENT_INFO";
    public static final String ECG_ANALYSE_PARAM = "ECG_ANALYSE_PARAM";
    public static String ECGDATA_PDF_LB = Environment
            .getExternalStorageDirectory().getAbsolutePath() + "/ECGDATA/LB_PDF/";

    public static String date2String(long time) {
        Date date = new Date(time);
        return sdfYMDHM.format(date);
    }

    public static String data2String(int[] data) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < data.length; i++) {
            sb.append(data[i] + " ");
        }
        return sb.toString();
    }

    public static String data2String(List<Integer> data) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < data.size(); i++) {
            sb.append(data.get(i) + " ");
        }
        return sb.toString();
    }


    public static String data2String(short[] data) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < data.length; i++) {
            sb.append(data[i] + " ");
        }
        return sb.toString();
    }

    public static String getCpuID() {
        String macSerial = null;
        String str = "";

        try {
            Process pp = Runtime.getRuntime().exec("cat /proc/cpuinfo");
            InputStreamReader ir = new InputStreamReader(pp.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);

            for (; null != str; ) {
                str = input.readLine();
                if (str != null) {
                    macSerial = str.trim();// 去空格
                    break;
                }
            }
        } catch (IOException ex) {
            // 赋予默认值
            ex.printStackTrace();
        }
        return macSerial;
    }

    /**
     * 将点的集合改为数组
     **/
    public static int[] lists2Datas(List<int[]> d) {
        int[] data = new int[d.size() * 12];
        for (int i = 0; i < d.size(); i++) {
            for (int j = 0; j < 12; j++) {
                data[i * 12 + j] = d.get(i)[j];
            }
        }
        return data;
    }

    public static void writeException(Context context, Exception e, String type) {
        StringBuffer sb = new StringBuffer();
        Writer writer = new StringWriter();
        PrintWriter printWriter = new PrintWriter(writer);
        e.printStackTrace(printWriter);
        Throwable cause = e.getCause();
        while (cause != null) {
            cause.printStackTrace(printWriter);
            cause = cause.getCause();
        }
        printWriter.close();
        String result = writer.toString();
        sb.append(result);
        String fileName = context.getExternalCacheDir() + "/" + "auto_manual.txt";
        writException2File(fileName, "\n \n" + type + "\n\n+++++" + sb.toString() + " 时间：" + new Date().toString() + "\n \n");
    }

    public static void writeLog(Context context, String content) {
        String fileName = context.getExternalCacheDir() + "/" + "usb_new.txt";
        writException2File(fileName, "\n \n" + content + "  +++++" + " 时间：" + new Date().toString() + "\n \n");
    }

    public static void writeBytes(Context context, String content) {
        String fileName = context.getExternalCacheDir() + "/" + "usb_init_" + System.currentTimeMillis() + ".txt";
        writException2File(fileName, content);
    }

    public static void writeBytes_1(Context context, String content) {
        String fileName = context.getExternalCacheDir() + "/" + "usb_20_" + System.currentTimeMillis() + ".txt";
        writException2File(fileName, content);
    }

    public static void writeBytes_2(Context context, String content) {
        String fileName = context.getExternalCacheDir() + "/" + "usb_huanhang_" + System.currentTimeMillis() + ".txt";
        writException2File(fileName, content);
    }

    public static void writeBytes_Ble(Context context, String content) {
        String fileName = context.getExternalCacheDir() + "/" + "byte_ble" + ".txt";
        writException2File(fileName, content);
    }

    public static void writeInts_Ble(Context context, String content) {
        String fileName = context.getExternalCacheDir() + "/" + "int_ble" + ".txt";
        writException2File(fileName, content);
    }

    public static void writeBytes_Usb(Context context, String content) {
        String fileName = context.getExternalCacheDir() + "/" + "byte_usb" + ".txt";
        writException2File(fileName, content);
    }

    public static void writeBytes_Usb1(Context context, String content) {
        String fileName = context.getExternalCacheDir() + "/" + "byte_usb1" + ".txt";
        writException2File(fileName, content);
    }

    public static void writeBytes_Usb2(Context context, String content) {
        String fileName = context.getExternalCacheDir() + "/" + "byte_usb2" + ".txt";
        writException2File(fileName, content);
    }

    /**
     * 追加文件：使用FileOutputStream，在构造FileOutputStream时，把第二个参数设为true
     *
     * @param file
     * @param content
     */
    public static void writException2File(String file, String content) {
        BufferedWriter out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(file, true)));
            out.write(content);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 如果是英文或者汉字返回true
     **/
    public static boolean verifyName(String userNameStr) {
        int result = -1;
        char[] ch = userNameStr.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            if (Pattern.matches(regEx, String.valueOf(ch[i]))) {//是汉字

            } else if (((ch[i] >= 'a' && ch[i] <= 'z') || (ch[i] >= 'A' && ch[i] <= 'Z'))) {//英文字母

            } else {
                result = 1;//包含不是汉字或者英文的字符时，跳出for循环。
                break;
            }
        }
        return result == 1 ? false : true;
    }

    public static String getSDPath() {
        File sdDir = null;
        boolean sdCardExist = Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED); // 判断sd卡是否存在
        if (sdCardExist) {
            sdDir = Environment.getExternalStorageDirectory();// 获取跟目录  
        }
        return sdDir.toString();
    }

    @SuppressLint("SimpleDateFormat")
    public static String getBirthDay(String idCardNo) {
        Date date = null;
        String subStr;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd");
        subStr = idCardNo.substring(6, 14);
        try {
            date = formatter.parse(subStr);
            return formatter2.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return "";
        }
    }

    @SuppressLint("SimpleDateFormat")
    public static int getAgeByBirthDay(String idCardNo) {
        int age = 60;
        Date date = new Date();
        try {
            String subStr;
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
            subStr = idCardNo.substring(6, 10);
            String current = formatter.format(date);
            int bd = Integer.valueOf(subStr);
            int crt = Integer.valueOf(current);
            age = crt - bd;
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return age;
    }

    /**
     * 从身份证号中获取性别
     **/
    @SuppressLint("DefaultLocale")
    public static String getGender(String idCardNo) {
        if (idCardNo == null || (idCardNo.length() != 15 && idCardNo.length() != 18)) {
            return null;
        }
        if (idCardNo.length() == 15 || idCardNo.length() == 18) {
            String lastValue = idCardNo.substring(idCardNo.length() - 2, idCardNo.length() - 1);
            int sex;
            sex = Integer.valueOf(lastValue) % 2;
            return sex == 0 ? "女" : "男";
        } else {
            return null;
        }
    }

    /**
     * 从身份证号中获取性别
     **/
    @SuppressLint("DefaultLocale")
    public static int getIntGender(String idCardNo) {//保证
        int sex = 0;
        if (idCardNo == null || (idCardNo.length() != 15 && idCardNo.length() != 18)) {
            return sex;
        }
        if (idCardNo.length() != 15 && idCardNo.length() != 18) {
            return sex;
        }

        String lastValue = idCardNo.substring(idCardNo.length() - 2, idCardNo.length() - 1);
        sex = Integer.valueOf(lastValue) % 2;
        if (sex == 0) {
            sex = 1;
            return sex;
        }
        if (sex == 1) {
            sex = 0;
            return sex;
        }
        return sex;
    }

    /**
     * 获取身份证号后六位
     **/
    public static String getIdCardPsd(String idCardNo) {
        int start = idCardNo.length() - 6;
        int end = idCardNo.length();
        return idCardNo.substring(start, end);
    }

    /**
     * 计算年龄
     */
    @SuppressLint("SimpleDateFormat")
    public static int getAge(Date date) {
        int age = 0;
        if (date == null) {
            return age;
        }
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String birthDay = sdf.format(date);
            // 得到当前的年份
            String cYear = sdf.format(new Date()).substring(0, 4);
            // 得到生日年份
            String birthYear = birthDay.substring(0, 4);

            age = Integer.parseInt(cYear) - Integer.parseInt(birthYear);
        } catch (Exception e) {
            e.printStackTrace();
            age = 0;
        }
        return age;
    }

    public static int getHourIntByDate(Date d) {
        int hour = 8;
        try {
            String hourStr = sdfH.format(d);
            hour = Integer.valueOf(hourStr);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return hour;
    }

    public static double weekDay2Double(String dateStr) {
        double d = 0;
        try {
            Date date = sdfYMDHm.parse(dateStr + " 23:59");
            d = date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return d;
    }

    public static long[] getMonthStartEndByMonth(String yearMonth) {
        String[] ym = yearMonth.split("-");
        int year = Integer.valueOf(ym[0]);
        int m = Integer.valueOf(ym[1]);
        int maxOfMonth = getDayOfMonth(year, m);
        Date startDate = null;
        Date endDate = null;
        try {
            startDate = sdfYMDHm.parse(yearMonth + "-01 00:00");
            endDate = sdfYMDHm.parse(yearMonth + "-" + maxOfMonth + " 23:59");
        } catch (ParseException e) {
            e.printStackTrace();
            startDate = new Date();
            endDate = new Date();
        }

        return new long[]{startDate.getTime(), endDate.getTime()};
    }

    /**
     * 将分变换成小时的小数
     *
     * @param d
     * @return
     */
    public static double strToDouble(Date d) {
        String h = sdfH.format(d);
        String m = sdfm.format(d);
        double hd = Double.valueOf(h);
        double md = Double.valueOf(m);
        BigDecimal b = new BigDecimal(md / 60);
        double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        return hd + f1;
    }

    /**
     * 将小时转换成天（月）的小数
     *
     * @param d
     * @return
     */
    public static double dayOfMonthToDouble(Date d) {
        String D = sdfD.format(d);
        String H = sdfH.format(d);
        String m = sdfm.format(d);
        double md = Double.valueOf(m);
        double dd = Double.valueOf(D);
        double hd = Double.valueOf(H);
        BigDecimal mb = new BigDecimal(md / 60);
        double f1 = mb.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        BigDecimal hb = new BigDecimal((hd + f1) / 24);
        double f2 = hb.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        return dd + f2;
    }


    public static double dayOfMonthToDouble2(Date d) {
        String D = sdfD.format(d);
        double dd = Double.valueOf(D);
        return dd;
    }

    /**
     * 将小时转换成天（周）的小数
     *
     * @param d
     * @return
     */
    public static double dayOfWeekToDouble(Date d) {
        String H = sdfH.format(d);
        String m = sdfm.format(d);
        double md = Double.valueOf(m);
        double hd = Double.valueOf(H);
        BigDecimal mb = new BigDecimal(md / 60);
        double f1 = mb.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
        BigDecimal hb = new BigDecimal((hd + f1) / 24);
        double f2 = hb.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
        return f2;
    }

    /**
     * 当前时间的月份
     **/
    public static int getMonthOfYear() {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        //获得当前时间的月份，月份从0开始所以结果要加1
        int month = calendar.get(Calendar.MONTH) + 1;
        return month;
    }

    public static String getMonthByCurrent(Date d) {
        String start;
        String year_month = sdfYM.format(d);
        start = year_month + "-01 00:00";
        return start;
    }

    /**
     * 月的最大天数
     **/
    public static int getMaxDayOfMonth(String d) {
        String[] ym = d.split("-");
        int y = Integer.valueOf(ym[0]);
        int m = Integer.valueOf(ym[1]);
        int max = getDayOfMonth(y, m - 1);
        return max;
    }

    /**
     * 获取当前月的天数
     **/
    public static int getDayOfMonth(int year, int month) {
        Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
        aCalendar.set(Calendar.YEAR, year);
        aCalendar.set(Calendar.MONTH, month);
        aCalendar.set(Calendar.DATE, 1);
        aCalendar.roll(Calendar.DATE, -1);
        int day = aCalendar.get(Calendar.DATE);
//		int day = aCalendar.getActualMaximum(Calendar.DATE);
        return day;
    }

    /**
     * 最近一周
     **/
    public static long getLastWeek() {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.add(GregorianCalendar.DATE, -6);
        Date date = calendar1.getTime();
        String s = sdfYMD.format(date);
        String start = s + " 00:00";
        Date startDate = null;
        try {
            startDate = sdfYMDHm.parse(start);
        } catch (ParseException e) {
            e.printStackTrace();
            startDate = calendar1.getTime();
        }
        return startDate.getTime();
    }


    /**
     * 获取最近一周
     **/
    @SuppressLint("UseSparseArrays")
    public static Map<Integer, String> getWeekLabel() {
        Map<Integer, String> map = new HashMap<Integer, String>();
        for (int i = 0; i < 7; i++) {
            Calendar calendar = Calendar.getInstance(Locale.CHINA);
            calendar.add(GregorianCalendar.DATE, i - 6);
            Date dd = new Date(calendar.getTimeInMillis());
            String dd1 = sdfYMDE.format(dd);
            map.put(i, dd1);
        }
        return map;
    }

    /**
     * 获取最近一周
     **/
    @SuppressLint("UseSparseArrays")
    public static List<String> getListWeekLabel() {
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < 7; i++) {
            Calendar calendar = Calendar.getInstance(Locale.CHINA);
            calendar.add(GregorianCalendar.DATE, i - 6);
            Date dd = new Date(calendar.getTimeInMillis());
            String dd1 = sdfE.format(dd);
            list.add(i, dd1);
        }
        return list;
    }

    @SuppressLint("UseSparseArrays")
    public static Map<Integer, String> getSugarType() {
        Map<Integer, String> map = new HashMap<Integer, String>();
        map.put(0, "早餐");//早餐前
        map.put(1, "");//早餐后1
        map.put(2, "");//早餐后2
        map.put(3, "午餐");//午餐前
        map.put(4, "");//午餐后1
        map.put(5, "");//午餐后2
        map.put(6, "晚餐");//晚餐前
        map.put(7, "");//晚餐后1
        map.put(8, "");//晚餐后2
        map.put(9, "睡前");//睡前
        map.put(10, "自定义");//自定义
        return map;
    }

    @SuppressLint("UseSparseArrays")
    public static List<String> getListSugarType() {
        List<String> list = new ArrayList<String>();
        list.add(0, "");
        list.add(1, "早餐");//早餐前
        list.add(2, "");//早餐后1
        list.add(3, "");//早餐后2
        list.add(4, "午餐");//午餐前
        list.add(5, "");//午餐后1
        list.add(6, "");//午餐后2
        list.add(7, "晚餐");//晚餐前
        list.add(8, "");//晚餐后1
        list.add(9, "");//晚餐后2
        list.add(10, "睡前");//睡前
        list.add(11, "其它");//其它
        return list;
    }

    public static double getDoubleByDate2(Map<Integer, String> map, Date date) {
        Iterator<Entry<Integer, String>> it = map.entrySet().iterator();
        double dayDouble = 0;
        while (it.hasNext()) {
            Entry<Integer, String> m = it.next();
            if (m.getValue().contains(sdfYMDE.format(date))) {
                double i = m.getKey();
                dayDouble = i;
                break;
            }
        }
        return dayDouble;
    }

    /**
     * 血压获取周对应的时间小数
     *
     * @param map
     * @param date
     **/
    public static double getPressDoubleByDate(Map<Integer, String> map, Date date) {
        Iterator<Entry<Integer, String>> it = map.entrySet().iterator();
        double dayDouble = 0;
        while (it.hasNext()) {
            Entry<Integer, String> m = it.next();
            if (m.getValue().contains(sdfYMDE.format(date))) {
                double i = m.getKey();
                dayDouble = i + dayOfWeekToDouble(date);
                break;
            }
        }
        return dayDouble;
    }


    public static void deleteECGFile(Long dateTime) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            String sdk = Environment.getExternalStorageDirectory().getAbsolutePath() + "/ECGDATA/XML/";
            File file = new File(sdk + sdf.format(new Date(dateTime)) + ".xml");
            file.delete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void deleteECGLongFile(long time) {
        String path = getSDPath() + "/ECGDATA/Data2Device/";
        File file = new File(path + time + "_h24.txt");
        if (file.exists()) {
            file.delete();
        }
        File[] files = new File[12];
        files[0] = new File(path + time + "_v1.txt");
        files[1] = new File(path + time + "_v2.txt");
        files[2] = new File(path + time + "_v3.txt");
        files[3] = new File(path + time + "_v4.txt");
        files[4] = new File(path + time + "_v5.txt");
        files[5] = new File(path + time + "_v6.txt");
        files[6] = new File(path + time + "_avf.txt");
        files[7] = new File(path + time + "_avl.txt");
        files[8] = new File(path + time + "_avr.txt");
        files[9] = new File(path + time + "_i.txt");
        files[10] = new File(path + time + "_ii.txt");
        files[11] = new File(path + time + "_iii.txt");
        for (int i = 0; i < files.length; i++) {
            if (files[i].exists()) {
                files[i].delete();
            }
        }
    }

    /**
     * 获取血糖月数的X轴
     **/
    public static List<String> getListMonthSugarType(int currentMonth) {
        List<String> list = new ArrayList<String>();
        if (currentMonth % 2 == 0) {//偶数天的月
            for (int i = 0; i <= currentMonth; i++) {
                if (i % 2 == 0) {
                    if (i < 10) {
                        list.add(" " + i);
                    } else {
                        list.add("" + i);
                    }
                } else {
                    list.add(" ");
                }
            }
            return list;
        }
        if (currentMonth % 2 == 1) {//奇数月
            for (int i = 0; i <= currentMonth; i++) {
                if (i % 2 == 0) {
                    list.add(" ");
                } else {
                    if (i < 10) {
                        list.add(" " + i);
                    } else {
                        list.add("" + i);
                    }
                }
            }
            return list;
        }
        return null;
    }

    /***
     * 将原始心电数据转化为 存储int数据的map集合
     */
    @SuppressLint("UseSparseArrays")
    public static Map<Integer, int[]> praseIntData(List<int[]> bufferDrawSnap) {
        Map<Integer, int[]> map = new HashMap<Integer, int[]>();
        int[] d1 = new int[bufferDrawSnap.size()];
        int[] d2 = new int[bufferDrawSnap.size()];
        int[] d3 = new int[bufferDrawSnap.size()];
        int[] d4 = new int[bufferDrawSnap.size()];
        int[] d5 = new int[bufferDrawSnap.size()];
        int[] d6 = new int[bufferDrawSnap.size()];
        int[] d7 = new int[bufferDrawSnap.size()];
        int[] d8 = new int[bufferDrawSnap.size()];
        int[] d9 = new int[bufferDrawSnap.size()];
        int[] d10 = new int[bufferDrawSnap.size()];
        int[] d11 = new int[bufferDrawSnap.size()];
        int[] d12 = new int[bufferDrawSnap.size()];
//		StringBuffer sb1 = new StringBuffer();
//		StringBuffer sb2 = new StringBuffer();
//		StringBuffer sb3 = new StringBuffer();
//		StringBuffer sb4 = new StringBuffer();
//		StringBuffer sb5 = new StringBuffer();
//		StringBuffer sb6 = new StringBuffer();
//		StringBuffer sb7 = new StringBuffer();
//		StringBuffer sb8 = new StringBuffer();
//		StringBuffer sb9 = new StringBuffer();
//		StringBuffer sb10 = new StringBuffer();
//		StringBuffer sb11 = new StringBuffer();
//		StringBuffer sb12 = new StringBuffer();
        for (int j = 0; j < bufferDrawSnap.size(); j++) {
            for (int i = 0; i < 12; i++) {
                if (i == 0) {
                    d1[j] = bufferDrawSnap.get(j)[i];
                }
                if (i == 1) {
                    d2[j] = bufferDrawSnap.get(j)[i];
                }
                if (i == 2) {
                    d3[j] = bufferDrawSnap.get(j)[i];
                }
                if (i == 3) {
                    d4[j] = bufferDrawSnap.get(j)[i];
                }
                if (i == 4) {
                    d5[j] = bufferDrawSnap.get(j)[i];
                }
                if (i == 5) {
                    d6[j] = bufferDrawSnap.get(j)[i];
                }
                if (i == 6) {
                    d7[j] = bufferDrawSnap.get(j)[i];
                }
                if (i == 7) {
                    d8[j] = bufferDrawSnap.get(j)[i];
                }
                if (i == 8) {
                    d9[j] = bufferDrawSnap.get(j)[i];
                }
                if (i == 9) {
                    d10[j] = bufferDrawSnap.get(j)[i];
                }
                if (i == 10) {
                    d11[j] = bufferDrawSnap.get(j)[i];
                }
                if (i == 11) {
                    d12[j] = bufferDrawSnap.get(j)[i];
                }
            }
        }
        map.put(0, d1);
        map.put(1, d2);
        map.put(2, d3);
        map.put(3, d4);
        map.put(4, d5);
        map.put(5, d6);
        map.put(6, d7);
        map.put(7, d8);
        map.put(8, d9);
        map.put(9, d10);
        map.put(10, d11);
        map.put(11, d12);
        return map;
    }

    public static String datas2String(byte[] datas, int type) {
        StringBuffer sb = new StringBuffer();
        if (type == 0) {
            short[] shortDatas = ByteDataUtil.toShortArray(ZipUtil.unGZip(datas));
            for (int i = 0; i < shortDatas.length; i++) {
                sb.append(shortDatas[i] + " ");
            }
        } else {
            int[] intDatas = ByteDataUtil.toIntArray(ZipUtil.unGZip(datas));
            for (int i = 0; i < intDatas.length; i++) {
                sb.append(intDatas[i] + " ");
            }
        }

        return sb.toString();
    }

    public static Map<String, String> parseMapDataToString(Map<String, Object> map) {
        Map<String, String> resultMap = new HashMap<>();
        Integer[] d0 = (Integer[]) map.get("ecgDataBuffer0");
        Integer[] d1 = (Integer[]) map.get("ecgDataBuffer1");
        Integer[] d2 = (Integer[]) map.get("ecgDataBuffer2");
        Integer[] d3 = (Integer[]) map.get("ecgDataBuffer3");
        Integer[] d4 = (Integer[]) map.get("ecgDataBuffer4");
        Integer[] d5 = (Integer[]) map.get("ecgDataBuffer5");
        Integer[] d6 = (Integer[]) map.get("ecgDataBuffer6");
        Integer[] d7 = (Integer[]) map.get("ecgDataBuffer7");
        Integer[] d8 = (Integer[]) map.get("ecgDataBuffer8");
        Integer[] d9 = (Integer[]) map.get("ecgDataBuffer9");
        Integer[] d10 = (Integer[]) map.get("ecgDataBuffer10");
        Integer[] d11 = (Integer[]) map.get("ecgDataBuffer11");

        StringBuffer sb1 = new StringBuffer();
        StringBuffer sb2 = new StringBuffer();
        StringBuffer sb3 = new StringBuffer();
        StringBuffer sb4 = new StringBuffer();
        StringBuffer sb5 = new StringBuffer();
        StringBuffer sb6 = new StringBuffer();
        StringBuffer sb7 = new StringBuffer();
        StringBuffer sb8 = new StringBuffer();
        StringBuffer sb9 = new StringBuffer();
        StringBuffer sb10 = new StringBuffer();
        StringBuffer sb11 = new StringBuffer();
        StringBuffer sb12 = new StringBuffer();
        Log.e("TAG", "switch:d0.length=" + d0.length);
        Log.e("TAG", "switch:d11.length=" + d11.length);
        for (Integer integer : d0) {
            sb1.append(integer).append(" ");
        }
        for (Integer integer : d1) {
            sb2.append(integer).append(" ");
        }
        for (Integer integer : d2) {
            sb3.append(integer).append(" ");
        }
        for (Integer integer : d3) {
            sb4.append(integer).append(" ");
        }
        for (Integer integer : d4) {
            sb5.append(integer).append(" ");
        }
        for (Integer integer : d5) {
            sb6.append(integer).append(" ");
        }
        for (Integer integer : d6) {
            sb7.append(integer).append(" ");
        }
        for (Integer integer : d7) {
            sb8.append(integer).append(" ");
        }
        for (Integer element : d8) {
            sb9.append(element).append(" ");
        }
        for (Integer item : d9) {
            sb10.append(item).append(" ");
        }
        for (Integer value : d10) {
            sb11.append(value).append(" ");
        }
        for (Integer integer : d11) {
            sb12.append(integer).append(" ");
        }
        resultMap.put("Ch0", sb1.toString());
        resultMap.put("Ch1", sb2.toString());
        resultMap.put("Ch2", sb3.toString());
        resultMap.put("Ch3", sb4.toString());
        resultMap.put("Ch4", sb5.toString());
        resultMap.put("Ch5", sb6.toString());
        resultMap.put("Ch6", sb7.toString());
        resultMap.put("Ch7", sb8.toString());
        resultMap.put("Ch8", sb9.toString());
        resultMap.put("Ch9", sb10.toString());
        resultMap.put("Ch10", sb11.toString());
        resultMap.put("Ch11", sb12.toString());
        return resultMap;
    }

    public static Map<String, String> praseIntDataToString(List<int[]> bufferDrawSnap) {
        Map<String, String> map = new HashMap<>();
        StringBuffer sb1 = new StringBuffer();
        StringBuffer sb2 = new StringBuffer();
        StringBuffer sb3 = new StringBuffer();
        StringBuffer sb4 = new StringBuffer();
        StringBuffer sb5 = new StringBuffer();
        StringBuffer sb6 = new StringBuffer();
        StringBuffer sb7 = new StringBuffer();
        StringBuffer sb8 = new StringBuffer();
        StringBuffer sb9 = new StringBuffer();
        StringBuffer sb10 = new StringBuffer();
        StringBuffer sb11 = new StringBuffer();
        StringBuffer sb12 = new StringBuffer();
        for (int j = 0; j < bufferDrawSnap.size(); j++) {
            for (int i = 0; i < 12; i++) {
                if (i == 0) {
                    sb1.append(bufferDrawSnap.get(j)[i] + " ");
                }
                if (i == 1) {
                    sb2.append(bufferDrawSnap.get(j)[i] + " ");
                }
                if (i == 2) {
                    sb3.append(bufferDrawSnap.get(j)[i] + " ");
                }
                if (i == 3) {
                    sb4.append(bufferDrawSnap.get(j)[i] + " ");
                }
                if (i == 4) {
                    sb5.append(bufferDrawSnap.get(j)[i] + " ");
                }
                if (i == 5) {
                    sb6.append(bufferDrawSnap.get(j)[i] + " ");
                }
                if (i == 6) {
                    sb7.append(bufferDrawSnap.get(j)[i] + " ");
                }
                if (i == 7) {
                    sb8.append(bufferDrawSnap.get(j)[i] + " ");
                }
                if (i == 8) {
                    sb9.append(bufferDrawSnap.get(j)[i] + " ");
                }
                if (i == 9) {
                    sb10.append(bufferDrawSnap.get(j)[i] + " ");
                }
                if (i == 10) {
                    sb11.append(bufferDrawSnap.get(j)[i] + " ");
                }
                if (i == 11) {
                    sb12.append(bufferDrawSnap.get(j)[i] + " ");
                }
            }
        }
        map.put("Ch0", sb1.toString());
        map.put("Ch1", sb2.toString());
        map.put("Ch2", sb3.toString());
        map.put("Ch3", sb4.toString());
        map.put("Ch4", sb5.toString());
        map.put("Ch5", sb6.toString());
        map.put("Ch6", sb7.toString());
        map.put("Ch7", sb8.toString());
        map.put("Ch8", sb9.toString());
        map.put("Ch9", sb10.toString());
        map.put("Ch10", sb11.toString());
        map.put("Ch11", sb12.toString());
        return map;
    }

    /**
     * 将点的集合改为数组
     **/
    public static int[] getEcgDataINTs(List<int[]> d) {
        long start = System.currentTimeMillis();
        int[] data = new int[d.size() * 12];
        StringUtils.writException2File("/sdcard/manual.txt", "\n 新数组的length：" + data.length);
        int index = 0;
        try {
            for (int i = 0; i < d.size(); i++) {
                for (int j = 0; j < 12; j++) {
                    index = i * 12 + j;
                    data[i * 12 + j] = d.get(i)[j];
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            StringUtils.writException2File("/sdcard/manual.txt", "\n 赋值下标：" + (index));
        }
        long end = System.currentTimeMillis();
        StringUtils.writException2File("/sdcard/manual.txt", "\n 重新赋值时间：" + (end - start));
        return data;
    }

    /**
     * 将点的集合改为导的集合
     **/
    public static List<int[]> getEcgData(List<int[]> data) {
        List<int[]> des = new ArrayList<int[]>();
        int[] d1 = new int[data.size()];
        int[] d2 = new int[data.size()];
        int[] d3 = new int[data.size()];
        int[] d4 = new int[data.size()];
        int[] d5 = new int[data.size()];
        int[] d6 = new int[data.size()];
        int[] d7 = new int[data.size()];
        int[] d8 = new int[data.size()];
        int[] d9 = new int[data.size()];
        int[] d10 = new int[data.size()];
        int[] d11 = new int[data.size()];
        int[] d12 = new int[data.size()];
        for (int j = 0; j < data.size(); j++) {
            for (int i = 0; i < LEADNUM; i++) {
                if (i == 0) {
                    d1[j] = data.get(j)[i];
                }
                if (i == 1) {
                    d2[j] = data.get(j)[i];
                }
                if (i == 2) {
                    d3[j] = data.get(j)[i];
                }
                if (i == 3) {
                    d4[j] = data.get(j)[i];
                }
                if (i == 4) {
                    d5[j] = data.get(j)[i];
                }
                if (i == 5) {
                    d6[j] = data.get(j)[i];
                }
                if (i == 6) {
                    d7[j] = data.get(j)[i];
                }
                if (i == 7) {
                    d8[j] = data.get(j)[i];
                }
                if (i == 8) {
                    d9[j] = data.get(j)[i];
                }
                if (i == 9) {
                    d10[j] = data.get(j)[i];
                }
                if (i == 10) {
                    d11[j] = data.get(j)[i];
                }
                if (i == 11) {
                    d12[j] = data.get(j)[i];
                }
            }
        }
        des.add(d1);
        des.add(d2);
        des.add(d3);
        des.add(d4);
        des.add(d5);
        des.add(d6);
        des.add(d7);
        des.add(d8);
        des.add(d9);
        des.add(d10);
        des.add(d11);
        des.add(d12);
        return des;
    }

    @SuppressLint("UseSparseArrays")
    public static Map<Integer, Integer[]> praseIntegerData(Map<String, Object> map) {
        Integer[] d0 = (Integer[]) map.get("ecgDataBuffer0");
        Integer[] d1 = (Integer[]) map.get("ecgDataBuffer1");
        Integer[] d2 = (Integer[]) map.get("ecgDataBuffer2");
        Integer[] d3 = (Integer[]) map.get("ecgDataBuffer3");
        Integer[] d4 = (Integer[]) map.get("ecgDataBuffer4");
        Integer[] d5 = (Integer[]) map.get("ecgDataBuffer5");
        Integer[] d6 = (Integer[]) map.get("ecgDataBuffer6");
        Integer[] d7 = (Integer[]) map.get("ecgDataBuffer7");
        Integer[] d8 = (Integer[]) map.get("ecgDataBuffer8");
        Integer[] d9 = (Integer[]) map.get("ecgDataBuffer9");
        Integer[] d10 = (Integer[]) map.get("ecgDataBuffer10");
        Integer[] d11 = (Integer[]) map.get("ecgDataBuffer11");

        Map<Integer, Integer[]> mapInt = new HashMap<Integer, Integer[]>();
        mapInt.put(0, d0);
        mapInt.put(1, d1);
        mapInt.put(2, d2);
        mapInt.put(3, d3);
        mapInt.put(4, d4);
        mapInt.put(5, d5);
        mapInt.put(6, d6);
        mapInt.put(7, d7);
        mapInt.put(8, d8);
        mapInt.put(9, d9);
        mapInt.put(10, d10);
        mapInt.put(11, d11);

        return mapInt;
    }

    public static int[] getMapEcgDataINTs(Map<String, Object> map) {
        Integer[] d0 = (Integer[]) map.get("ecgDataBuffer0");
        Integer[] d1 = (Integer[]) map.get("ecgDataBuffer1");
        Integer[] d2 = (Integer[]) map.get("ecgDataBuffer2");
        Integer[] d3 = (Integer[]) map.get("ecgDataBuffer3");
        Integer[] d4 = (Integer[]) map.get("ecgDataBuffer4");
        Integer[] d5 = (Integer[]) map.get("ecgDataBuffer5");
        Integer[] d6 = (Integer[]) map.get("ecgDataBuffer6");
        Integer[] d7 = (Integer[]) map.get("ecgDataBuffer7");
        Integer[] d8 = (Integer[]) map.get("ecgDataBuffer8");
        Integer[] d9 = (Integer[]) map.get("ecgDataBuffer9");
        Integer[] d10 = (Integer[]) map.get("ecgDataBuffer10");
        Integer[] d11 = (Integer[]) map.get("ecgDataBuffer11");

        int[] data = new int[d0.length * 12];

        for (int i = 0; i < d0.length; i++) {//d0
            data[i] = d0[i];
        }
        for (int i = 0; i < d0.length; i++) {//d1
            data[d0.length + i] = d1[i];
        }
        for (int i = 0; i < d0.length; i++) {//d2
            data[d0.length * 2 + i] = d2[i];
        }
        for (int i = 0; i < d0.length; i++) {//d3
            data[d0.length * 3 + i] = d3[i];
        }
        for (int i = 0; i < d0.length; i++) {//d4
            data[d0.length * 4 + i] = d4[i];
        }
        for (int i = 0; i < d0.length; i++) {//d5
            data[d0.length * 5 + i] = d5[i];
        }
        for (int i = 0; i < d0.length; i++) {//d6
            data[d0.length * 6 + i] = d6[i];
        }
        for (int i = 0; i < d0.length; i++) {//d7
            data[d0.length * 7 + i] = d7[i];
        }
        for (int i = 0; i < d0.length; i++) {//d8
            data[d0.length * 8 + i] = d8[i];
        }
        for (int i = 0; i < d0.length; i++) {//d9
            data[d0.length * 9 + i] = d9[i];
        }
        for (int i = 0; i < d0.length; i++) {//d10
            data[d0.length * 10 + i] = d10[i];
        }
        for (int i = 0; i < d0.length; i++) {//d11
            data[d0.length * 11 + i] = d11[i];
        }
        return data;
    }

    public static List<int[]> praseDatas(Map<String, Object> map) {
        Integer[] d0 = (Integer[]) map.get("ecgDataBuffer0");
        Integer[] d1 = (Integer[]) map.get("ecgDataBuffer1");
        Integer[] d2 = (Integer[]) map.get("ecgDataBuffer2");
        Integer[] d3 = (Integer[]) map.get("ecgDataBuffer3");
        Integer[] d4 = (Integer[]) map.get("ecgDataBuffer4");
        Integer[] d5 = (Integer[]) map.get("ecgDataBuffer5");
        Integer[] d6 = (Integer[]) map.get("ecgDataBuffer6");
        Integer[] d7 = (Integer[]) map.get("ecgDataBuffer7");
        Integer[] d8 = (Integer[]) map.get("ecgDataBuffer8");
        Integer[] d9 = (Integer[]) map.get("ecgDataBuffer9");
        Integer[] d10 = (Integer[]) map.get("ecgDataBuffer10");
        Integer[] d11 = (Integer[]) map.get("ecgDataBuffer11");
        List<int[]> list = new ArrayList<int[]>();
        for (int i = 0; i < d11.length; i++) {
            int[] datas = new int[12];
            datas[0] = d0[i];
            datas[1] = d1[i];
            datas[2] = d2[i];
            datas[3] = d3[i];
            datas[4] = d4[i];
            datas[5] = d5[i];
            datas[6] = d6[i];
            datas[7] = d7[i];
            datas[8] = d8[i];
            datas[9] = d9[i];
            datas[10] = d10[i];
            datas[11] = d11[i];
            list.add(datas);
        }
        return list;
    }


//	private int getYLead(int leadNum, int data){
//		int yValue = 0;
//		switch (leadNum) {
//		case 0:
//			yValue = (baseY[leadNum] + ( - data) / 128 * (screenHeight / 550) - 110);
//			break;
//		case 1:
//			yValue = (baseY[leadNum] + ( - data) / 128 * (screenHeight / 550) + 500);
//			break;
//		case 2:
//			yValue = (baseY[leadNum] + ( - data) / 128 * (screenHeight / 550) + 639);
//			break;
//		case 3:
//			yValue = (baseY[leadNum] + ( - data) / 128 * (screenHeight / 550) - 290);
//			break;
//		case 4:
//			yValue = (baseY[leadNum] + ( - data) / 128 * (screenHeight / 550) - 730);
//			break;
//		case 5:
//			yValue = (baseY[leadNum] + ( - data) / 128 * (screenHeight / 550) + 234);
//			break;
//		case 6:
//			yValue = (baseY[leadNum] + ( - data) / 128 * (screenHeight / 550));
//			break;
//		case 7:
//			yValue = (baseY[leadNum] + ( - data) / 128 * (screenHeight / 550) - 132);
//			break;
//		case 8:
//			yValue = (baseY[leadNum] + ( - data) / 128 * (screenHeight / 550) - 234);
//			break;
//		case 9:
//			yValue = (baseY[leadNum] + ( - data) / 128 * (screenHeight / 550) - 253);
//			break;
//		case 10:
//			yValue = (baseY[leadNum] + ( - data) / 128 * (screenHeight / 550) - 407);
//			break;
//		case 11:
//			yValue = (baseY[leadNum] + ( - data) / 128 * (screenHeight / 550) - 450);
//			break;
//
//		default:
//			break;
//		}
//		return yValue;
//	}

//	public static int getMaxOfMonth(Date d){
//		List<String> list31 = new ArrayList<String>();
//		list31.add("01");
//		list31.add("03");
//		list31.add("05");
//		list31.add("07");
//		list31.add("08");
//		list31.add("10");
//		list31.add("12");
//		
//		
//		List<String> list30 = new ArrayList<String>();
//		list30.add("04");
//		list30.add("06");
//		list30.add("09");
//		list31.add("11");
//		
//		String m = sdfM.format(d);
//		
//		if (list31.contains(m)) {
//			return 31;
//		}else if(list30.contains(m)){
//			return 30;
//		}else {
//			return 29;
//		}
//	}
}
